package com.moneywelcome.userserviceassembly.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moneywelcome.results.Result;
import com.moneywelcome.userserviceassembly.entity.UserLogin;
import com.moneywelcome.userserviceassembly.exception.UpdatePasswordExecption;
import com.moneywelcome.userserviceassembly.exception.UserLoginExecption;
import com.moneywelcome.userserviceassembly.mapper.UserMapper;
import com.moneywelcome.userserviceassembly.service.UserService;
import com.moneywelcome.userserviceassembly.util.MyBeanUtils;
import com.moneywelcome.userserviceassembly.util.Sendsms;
import com.woniu.demo.Dto.PageDto;
import com.woniu.demo.Dto.PageLogininfoDto;
import com.woniu.demo.Dto.UpdatePasswordDto;
import com.woniu.demo.Dto.UserLoginDto;
import com.woniu.demo.param.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserLogin> implements UserService {

    @Autowired
    private StringRedisTemplate srt;

    @Resource
    private UserMapper userMapper;

    @Override//验证验证码
    public Result checkcode(PhoneRegisterParam phoneRegisterParam) {
        System.out.println(phoneRegisterParam);
        String code = srt.opsForValue().get("woniu:demo:phone:" + phoneRegisterParam.getUserPhone());
        if (code == null) return Result.fail("验证码不存在");
        if (!code.equals(phoneRegisterParam.getUserCode())) return Result.fail("验证码错误");
        return Result.success("200","验证码验证成功",null);
    }

    //账号密码登录
    @Override
    public UserLoginDto userLogin(UserAccountLoginParam userAccountLoginParam) {
        System.out.println(userAccountLoginParam);
        //查询前台输入账号是否存在
        QueryWrapper<UserLogin> qw = new QueryWrapper();
        qw.eq("account",userAccountLoginParam.getUserAccount()).eq("password",userAccountLoginParam.getUserPassword());
        UserLogin one = getOne(qw);
        //如果不存在
        if(one==null){
            throw new UserLoginExecption(10000,"账号或密码错误");
            //如果账号状态为1，为冻结
        }else if(one.getAccountStatus()==1){
            throw new UserLoginExecption(10001,"该账号已经冻结");
            //如果账号状态为2，为黑名单
        }else if (one.getAccountStatus()==2){
            throw new UserLoginExecption(10002,"该账号为黑名单用户");
        }else {
            //账号正常登录，返回Dto
            UserLoginDto userLoginDto = new UserLoginDto();
            BeanUtils.copyProperties(one,userLoginDto);
            return userLoginDto;
        }
    }

    @Override//手机号发送短信，获取验证码
    public Result phonesetcode(String phone) {
        String rcode = srt.opsForValue().get("woniu:demo:phone:"+phone);
        if (rcode!=null){
            return Result.fail("验证码已存在不要重复发送");
        }
        Integer sendcode = Sendsms.Sendcode(phone);
        srt.opsForValue().set("woniu:demo:phone:"+phone, String.valueOf(sendcode),30, TimeUnit.SECONDS);
        return Result.success("发送成功");
    }

   //获取登录对象昵称
   public Result loadUserRoleNickname(String phone) {
       QueryWrapper<UserLogin> qw = new QueryWrapper();
       qw.eq("phone",phone);
       UserLogin userLogin =userMapper.selectOne(qw);
       return Result.success("200","登陆成功",userLogin);
    }

    //分页查询用户信息
    @Override
    public PageDto findUser(PageFindUserParam pageFindUserParam) {
        QueryWrapper<UserLogin> wrapper = new QueryWrapper<>();
        //查询条件
        if (pageFindUserParam.getAccount()!= null) {
            wrapper.and(
                    wq ->
                            wq.like("account", "%" + pageFindUserParam.getAccount() + "%").or()
                                    .like("phone", "%" + pageFindUserParam.getAccount() + "%").or()
                                    .like("register_time", "%" + pageFindUserParam.getAccount() + "%")
            );
        }
        //分页结果排序降序
        wrapper.orderByDesc("user_id");
        Page<UserLogin> page =page(new Page<UserLogin>(pageFindUserParam.getPageIndex(),pageFindUserParam.getPageSize()), wrapper);
        if (page.getRecords()!=null){
            List<PageLogininfoDto> s = MyBeanUtils.copyList(page.getRecords(), PageLogininfoDto.class);
            PageDto<PageLogininfoDto> dto = new PageDto<>();
            dto.setDate(s);
            dto.setPageSize(page.getPages());
            dto.setTotals(page.getTotal());
            return dto;
        }
        return null;
    }

    //忘记密码，修改密码
    @Override
    public UpdatePasswordDto updateUserInfo(UpdatePasswordParam updatePasswordParam) {
        String code = srt.opsForValue().get("woniu:demo:phone:" + updatePasswordParam.getPhone());
        if (code == null) throw new UpdatePasswordExecption(10050,"验证码不存在");
        if (!code.equals(updatePasswordParam.getCode())) throw new UpdatePasswordExecption(10051,"验证码错误");
        QueryWrapper<UserLogin> qw = new QueryWrapper<>();
        qw.eq("phone",updatePasswordParam.getPhone());
        UserLogin one = getOne(qw);
        one.setPassword(updatePasswordParam.getPassword());
        if (updateById(one)){
            return MyBeanUtils.copyProperties(one,UpdatePasswordDto.class);
        }
        return null;
    }

    //批量删除账号信息
    @Override
    public Boolean remUser(List<Integer> ids) {
        for (Integer i : ids){
            UserLogin byId = getById(i);
            if (removeById(i)) {
                return true;
            }
        }
        return true;
    }


}



